/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package unc.pds.facets;

import java.net.URLEncoder;
import unc.pds.model.Model;
import unc.pds.filemodel.FileRemote;
import unc.pds.filemodel.FolderRemote;
import unc.pds.model.UserRemote;

/**
 *
 * @author Mikhail
 */
public class FolderFacet implements Facet {
    private FolderRemote folder;
    private boolean isOwn;
    public FolderFacet(FolderRemote folder, boolean isOwn) {
        this.folder = folder;
        this.isOwn = isOwn;
    }
    public String draw() throws Exception {
        String result = "";
        FolderRemote[] folders = folder.getFolders();
        FileRemote[] files = folder.getFiles();
        result += "<h4>"+folder.getName()+" - "+folder.getAuthor().getNickname()+"</h4>";
        Model parent = folder.getParent();
        if (parent instanceof UserRemote) {
            result += "<p><a href=\"index.jsp?id="+parent.getKey()+"&view=folders\"><img src=\"img/back.png\" alt=\"Назад\"/></a></p>\n";
        } else if (parent instanceof FolderRemote) {
            result += "<p><a href=\"index.jsp?id="+parent.getKey()+"\"><img src=\"img/back.png\" alt=\"Назад\"/></a></p>\n";
        }
        for (int i = 0; i < folders.length; i++) {
            result += "<p><a href=\"index.jsp?id="+folders[i].getKey()+"\">"+folders[i].getName()+"</a>";
            if (isOwn) {
                result += " <a href=\"filesystem-processor.jsp?objid="+folders[i].getKey()+"&action=delete\"><img src=\"img/delete.png\" alt=\"Удалить\"/></a>";
            }
            result += "</p>\n";
        }
        for (int i = 0; i < files.length; i++) {
            result += "<p><a href=\"index.jsp?id="+files[i].getKey()+"\">"+files[i].getName()+"</a> - <i>"+DateFormatter.format(files[i].getDate())+"</i>"
                    + " <a href=\"download.jsp?id="+files[i].getKey()+"/"+URLEncoder.encode(files[i].getName(), "UTF-8")+"\"><img src=\"img/download.png\" alt=\"Загрузить\"/></a>";
            if (isOwn) {
                result += " <a href=\"filesystem-processor.jsp?objid="+files[i].getKey()+"&action=delete\"><img src=\"img/delete.png\" alt=\"Удалить\"/></a>";
            }
            result += "</p>\n";
        }
        return result;
    }
    public boolean isSide() {
        return false;
    }
}
